import { defineStore } from 'pinia'

export const useUserInfoStore = defineStore('userInfo', {
    state: () => ({
        userInfo: null
    }),
    getters: {
        isLoggedIn: state => !!state.userInfo,
        avatar: state => state.userInfo?.avatar,
        deptName: state => state.userInfo?.mainDepartment,
        userId: state => state.userInfo?.userId,
        name: state => state.userInfo?.name,
        mainDepartmentId: state => state.userInfo?.mainDepartmentId
    },
    actions: {
        setUserInfo(userInfo) {
            this.$patch({ userInfo })   // ✅ 安全写法
        },
        clearUserInfo() {
            this.$patch({ userInfo: null })
        }
    }
})
